package net.gcode.sdkscheduleparser.core.write;


import java.net.SocketException;
import java.util.Vector;

import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.TimeZone;
import net.fortuna.ical4j.model.TimeZoneRegistry;
import net.fortuna.ical4j.model.TimeZoneRegistryFactory;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.component.VTimeZone;
import net.fortuna.ical4j.model.property.CalScale;
import net.fortuna.ical4j.model.property.Description;
import net.fortuna.ical4j.model.property.Location;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Uid;
import net.fortuna.ical4j.model.property.Version;
import net.fortuna.ical4j.util.UidGenerator;
import net.gcode.sdkscheduleparser.core.Modul;
import net.gcode.sdkscheduleparser.core.Stdplan;
import net.gcode.sdkscheduleparser.core.Vorlesung;
import net.gcode.sdkscheduleparser.intface.ScheduleExport;

public class Export_cal implements ScheduleExport{

	@Override
	public String ExportConverter(Stdplan stundenplan, boolean utc) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String ExportConverter(Vector<Modul> module, boolean utc) {
		Calendar cal =this.buildCal();
		
		for (Modul modul : module) {
			for (Vorlesung vorlesung : modul.getVorlesungen()) {
				cal.getComponents().add( buildVorlesung(false, vorlesung));
			}
		}
		System.out.println(cal);
		return cal.toString();
	}
	private Calendar buildCal(){
		Calendar calendar = new Calendar();
		calendar.getProperties().add(new ProdId("-//Florian Mueller//iCal4j 1.0//EN"));
		calendar.getProperties().add(Version.VERSION_2_0);
		calendar.getProperties().add(CalScale.GREGORIAN);
		return calendar;
	}
	private VEvent buildVorlesung(boolean utc, Vorlesung vorlesung) {
		
		// Create a TimeZone
		TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
		TimeZone timezone = registry.getTimeZone("Europe/Berlin");
		VTimeZone tz = timezone.getVTimeZone();

	

		// Create the event
		DateTime start = new DateTime(vorlesung.getStartDate().getTime());
		DateTime end = new DateTime(vorlesung.getEndDate().getTime());
		VEvent meeting = new VEvent(start, end, vorlesung.getBez());
		
		
		meeting.getProperties().add(new Location(vorlesung.getRaum()));
		
		meeting.getProperties().add(new Description(vorlesung.getBeschreibung()));
		// add timezone info..
		meeting.getProperties().add(tz.getTimeZoneId());

		// generate unique identifier..
		UidGenerator ug;
		Uid uid = null;
		try {
			ug = new UidGenerator("uidGen");
			 uid = ug.generateUid();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		meeting.getProperties().add(uid);
		
		return meeting;

	}

	@Override
	public String getName() {
		return "iCal-Dateien neu";
	}

	@Override
	public String getExtension() {
		
		return "ics";
	}

}
